package com.example.yuejianapp14.Activity.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejianapp14.Activity.Activity.ActActivity;
import com.example.yuejianapp14.Activity.Activity.ClassActivity;
import com.example.yuejianapp14.Activity.Activity.ClubActivity;
import com.example.yuejianapp14.Activity.Activity.GameActivity;
import com.example.yuejianapp14.Activity.JavaBean.Search;
import com.example.yuejianapp14.R;

import java.util.List;

/**
 * Created by 张森 on 2017/11/13.
 */

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    public SearchAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.searchcard,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Search entity = (Search) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        if (entity.getActivity()!=null) {
            viewHolder.searchname.setText(entity.getActivity());
            viewHolder.searchtype.setText("活动");
        }else if (entity.getClubname()!=null){
            viewHolder.searchname.setText(entity.getClubname());
            viewHolder.searchtype.setText("俱乐部");
        }else if (entity.getCompetition()!=null){
            viewHolder.searchname.setText(entity.getCompetition());
            viewHolder.searchtype.setText("比赛");
        }else if (entity.getClassname()!=null){
            viewHolder.searchname.setText(entity.getClassname());
            viewHolder.searchtype.setText("培训");
        }
        viewHolder.serach_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = viewHolder.searchtype.getText().toString();
                if (type.equals("活动")){
                    Intent intent = new Intent(mContext, ActActivity.class);
                    intent.putExtra("actid",entity.getActivityid());
                    mContext.startActivity(intent);
                }else if (type.equals("俱乐部")){
                    Intent intent = new Intent(mContext,ClubActivity.class);
                    intent.putExtra("clubid",entity.getClubid());
                    intent.putExtra("club_name",entity.getClubname());
                    intent.putExtra("img",entity.getClubpic());
                    mContext.startActivity(intent);
                }else if (type.equals("比赛")){
                    Intent intent = new Intent(mContext, GameActivity.class);
                    intent.putExtra("url",entity.getLink());
                    intent.putExtra("gameid",entity.getCompetitionid());
                    mContext.startActivity(intent);
                }else if (type.equals("培训")){
                    Intent intent = new Intent(mContext, ClassActivity.class);
                    intent.putExtra("classid",entity.getClassid());
                    mContext.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView searchname,searchtype;
        LinearLayout serach_container;

        public ViewHolder(View itemView) {
            super(itemView);
            searchname = (TextView) itemView.findViewById(R.id.searchname);
            searchtype = (TextView) itemView.findViewById(R.id.searchtype);
            serach_container= (LinearLayout)itemView.findViewById(R.id.serach_container);
        }
    }
}
